﻿// <copyright file="TestTimerTrigger.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-05-27</date>
// <summary>TestTimerTrigger class definition</summary>

namespace Test
{
    using System.ComponentModel.Composition;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// Test the TimerTrigger
    /// </summary>
    [TestClass]
    public class TestTimerTrigger : SilverlightTest
    {
        #region Fields
        /// <summary>
        /// the test control
        /// </summary>
        private TesttimerTriggerControl control;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the web analytics events
        /// </summary>
        [Import]
        public WebAnalyticsEvents Events { get; set; }
        #endregion

        #region Methods

        /// <summary>
        /// Satisfy the MEF imports, create the test control, and add it to the test panel
        /// </summary>
        [TestInitialize]
        public void Initialize()
        {
            CompositionInitializer.SatisfyImports(this);

            this.control = new TesttimerTriggerControl();

            this.TestPanel.Children.Add(this.control);
        }

        /// <summary>
        /// Remove the control
        /// </summary>
        [TestCleanup]
        public void Cleanup()
        {
            this.TestPanel.Children.Remove(this.control);

            this.control = null;
        }

        /// <summary>
        /// Test that a TimerTrigger sends a "Timer" event
        /// </summary>
        [Asynchronous, TestMethod]
        public void TestTimer()
        {
            this.Events.TrackEvent += new System.EventHandler<AnalyticsEventArgs>(delegate(object sender, AnalyticsEventArgs e)
                {
                    if (e.AnalyticsEvent.Name == "Timer")
                    {
                        EnqueueCallback(() => Assert.AreEqual("LayoutRoot", e.AnalyticsEvent.ObjectName));
                        EnqueueCallback(() => Assert.AreEqual("TestCategory", e.AnalyticsEvent.Category));
                        EnqueueCallback(() => Assert.AreEqual(5, e.AnalyticsEvent.Value));

                        EnqueueTestComplete();
                    }
                });
        }

        #endregion
    }
}